package com.ilovepostcard;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ilovepostcard.controller.ServerAsyncTask;
import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.controller.server.PostcardResult;
import com.ilovepostcard.controller.server.ServerFactory;
import com.ilovepostcard.model.Comment;
import com.ilovepostcard.model.User;
import com.ilovepostcard.utils.StringUtils;
import com.ilovepostcard.utils.Utils;

public class CommentActivity extends BaseActivity {

    long pid;
    int pageIndex = 0;
    ArrayList<Comment> commentList = new ArrayList<Comment>();
    CommentAdapter adapter = new CommentAdapter();
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pid = getIntent().getLongExtra("pid", 0);
        setContentView(R.layout.activity_comment);
        ListView listView = (ListView) findViewById(R.id.comment_listview);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == adapter.getCount() - 1)
                    loadNextPage();
            }

        });
        
        final EditText commentEditText = (EditText) findViewById(R.id.comment_edittext);
        View send = findViewById(R.id.comment_send);
        send.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                String commentString = commentEditText.getText().toString();
                commentString = commentString.trim();
                if (StringUtils.isEmpty(commentString))
                    return;
                final String body = commentString;
                if (!Utils.isCommentLegal(CommentActivity.this, body)) {
                    Toast.makeText(CommentActivity.this, "内容含有敏感信息，不能发布。", Toast.LENGTH_SHORT).show();
                    return;
                }
                new ServerAsyncTask(CommentActivity.this) {
                    
                    User user;
                    
                    @Override
                    protected void onSuccess(PostcardResult result) {
                        commentEditText.setText("");
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.hideSoftInputFromWindow(commentEditText.getWindowToken(), 0);
                        Comment comment = new Comment();
                        comment.Nickname = user.Nickname;
                        comment.Body = body;
                        commentList.add(0, comment);
                        adapter.notifyDataSetChanged();
                        Toast.makeText(context, R.string.success_comment, Toast.LENGTH_SHORT).show();
                    }
                    
                    @Override
                    protected PostcardResult doRequest() {
                        user = UserController.getDetailUser(getApplicationContext());
                        return ServerFactory.getServer().newPostcardComment(user.UserID, pid, body);
                    }
                }.execute();
            }
        });
        
        loadNextPage();
    }
    
    private void loadNextPage() {
        new ServerAsyncTask(CommentActivity.this) {
            
            @Override
            protected void onSuccess(PostcardResult result) {
                pageIndex++;
                commentList.addAll(result.getPostcardCommentList());
                adapter.notifyDataSetChanged();
            }
            
            @Override
            protected PostcardResult doRequest() {
                return ServerFactory.getServer().getPostcardComments(pid, (pageIndex+1), 10);
            }
        }.execute();
    }
    
    private class CommentAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return commentList.size() + 1;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == getCount() - 1) {
                return View.inflate(CommentActivity.this, R.layout.show_more, null);
            }
            Comment comment = commentList.get(position);
            View view = View.inflate(CommentActivity.this, R.layout.comment_row, null);
            TextView nick = (TextView) view.findViewById(R.id.comment_nickname);
            TextView content = (TextView) view.findViewById(R.id.comment_content);
            nick.setText(comment.Nickname+"：");
            content.setText(comment.Body);
            return view;
        }
        
    }

}
